//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.newdemo.view.image.svg;

import android.util.Log;

import com.example.newdemo.view.image.svg.SVG.Group;
import com.example.newdemo.view.image.svg.SVG.Style;
import com.example.newdemo.view.image.svg.SVG.SvgContainer;
import com.example.newdemo.view.image.svg.SVG.SvgElementBase;
import com.example.newdemo.view.image.svg.SVG.SvgObject;
import com.example.newdemo.view.image.svg.SVGParser.TextScanner;

import org.xml.sax.SAXException;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class CSSParser {
    private static final String TAG = "AndroidSVG CSSParser";
    private static final String ID = "id";
    private static final String CLASS = "class";
    private CSSParser.MediaType rendererMediaType = null;
    private boolean inMediaRule = false;

    public CSSParser(CSSParser.MediaType rendererMediaType) {
        this.rendererMediaType = rendererMediaType;
    }

    public CSSParser.Ruleset parse(String sheet) throws SAXException {
        CSSParser.CSSTextScanner scan = new CSSParser.CSSTextScanner(sheet);
        scan.skipWhitespace();
        return this.parseRuleset(scan);
    }

    public static boolean mediaMatches(String mediaListStr, CSSParser.MediaType rendererMediaType) throws SAXException {
        CSSParser.CSSTextScanner scan = new CSSParser.CSSTextScanner(mediaListStr);
        scan.skipWhitespace();
        List<CSSParser.MediaType> mediaList = parseMediaList(scan);
        if (!scan.empty()) {
            throw new SAXException("Invalid @media type list");
        } else {
            return mediaMatches(mediaList, rendererMediaType);
        }
    }

    private static void warn(String format, Object... args) {
        Log.w("AndroidSVG CSSParser", String.format(format, args));
    }

    private static boolean mediaMatches(List<CSSParser.MediaType> mediaList, CSSParser.MediaType rendererMediaType) {
        Iterator var3 = mediaList.iterator();

        CSSParser.MediaType type;
        do {
            if (!var3.hasNext()) {
                return false;
            }

            type = (CSSParser.MediaType)var3.next();
        } while(type != CSSParser.MediaType.all && type != rendererMediaType);

        return true;
    }

    private static List<CSSParser.MediaType> parseMediaList(CSSParser.CSSTextScanner scan) throws SAXException {
        ArrayList typeList = new ArrayList();

        while(!scan.empty()) {
            String type = scan.nextToken(',');

            try {
                typeList.add(CSSParser.MediaType.valueOf(type));
            } catch (IllegalArgumentException var4) {
                throw new SAXException("Invalid @media type list");
            }

            if (!scan.skipCommaWhitespace()) {
                break;
            }
        }

        return typeList;
    }

    private void parseAtRule(CSSParser.Ruleset ruleset, CSSParser.CSSTextScanner scan) throws SAXException {
        String atKeyword = scan.nextIdentifier();
        scan.skipWhitespace();
        if (atKeyword == null) {
            throw new SAXException("Invalid '@' rule in <style> element");
        } else {
            if (!this.inMediaRule && atKeyword.equals("media")) {
                List<CSSParser.MediaType> mediaList = parseMediaList(scan);
                if (!scan.consume('{')) {
                    throw new SAXException("Invalid @media rule: missing rule set");
                }

                scan.skipWhitespace();
                if (mediaMatches(mediaList, this.rendererMediaType)) {
                    this.inMediaRule = true;
                    ruleset.addAll(this.parseRuleset(scan));
                    this.inMediaRule = false;
                } else {
                    this.parseRuleset(scan);
                }

                if (!scan.consume('}')) {
                    throw new SAXException("Invalid @media rule: expected '}' at end of rule set");
                }
            } else {
                warn("Ignoring @%s rule", atKeyword);
                this.skipAtRule(scan);
            }

            scan.skipWhitespace();
        }
    }

    private void skipAtRule(CSSParser.CSSTextScanner scan) {
        int depth = 0;

        while(!scan.empty()) {
            int ch = scan.nextChar();
            if (ch == 59 && depth == 0) {
                return;
            }

            if (ch == 123) {
                ++depth;
            } else if (ch == 125 && depth > 0) {
                --depth;
                if (depth == 0) {
                    return;
                }
            }
        }

    }

    private CSSParser.Ruleset parseRuleset(CSSParser.CSSTextScanner scan) throws SAXException {
        CSSParser.Ruleset ruleset = new CSSParser.Ruleset();

        while(!scan.empty()) {
            if (!scan.consume("<!--") && !scan.consume("-->")) {
                if (scan.consume('@')) {
                    this.parseAtRule(ruleset, scan);
                } else {
                    if (this.parseRule(ruleset, scan)) {
                        continue;
                    }
                    break;
                }
            }
        }

        return ruleset;
    }

    private boolean parseRule(CSSParser.Ruleset ruleset, CSSParser.CSSTextScanner scan) throws SAXException {
        List<CSSParser.Selector> selectors = this.parseSelectorGroup(scan);
        if (selectors != null && !selectors.isEmpty()) {
            if (!scan.consume('{')) {
                throw new SAXException("Malformed rule block in <style> element: missing '{'");
            } else {
                scan.skipWhitespace();
                Style ruleStyle = this.parseDeclarations(scan);
                scan.skipWhitespace();
                Iterator var6 = selectors.iterator();

                while(var6.hasNext()) {
                    CSSParser.Selector selector = (CSSParser.Selector)var6.next();
                    ruleset.add(new CSSParser.Rule(selector, ruleStyle));
                }

                return true;
            }
        } else {
            return false;
        }
    }

    private List<CSSParser.Selector> parseSelectorGroup(CSSParser.CSSTextScanner scan) throws SAXException {
        if (scan.empty()) {
            return null;
        } else {
            ArrayList<CSSParser.Selector> selectorGroup = new ArrayList(1);
            CSSParser.Selector selector = new CSSParser.Selector();

            while(!scan.empty() && scan.nextSimpleSelector(selector)) {
                if (scan.skipCommaWhitespace()) {
                    selectorGroup.add(selector);
                    selector = new CSSParser.Selector();
                }
            }

            if (!selector.isEmpty()) {
                selectorGroup.add(selector);
            }

            return selectorGroup;
        }
    }

    private Style parseDeclarations(CSSParser.CSSTextScanner scan) throws SAXException {
        Style ruleStyle = new Style();

        do {
            String propertyName = scan.nextIdentifier();
            scan.skipWhitespace();
            if (!scan.consume(':')) {
                break;
            }

            scan.skipWhitespace();
            String propertyValue = scan.nextPropertyValue();
            if (propertyValue == null) {
                break;
            }

            scan.skipWhitespace();
            if (scan.consume('!')) {
                scan.skipWhitespace();
                if (!scan.consume("important")) {
                    throw new SAXException("Malformed rule set in <style> element: found unexpected '!'");
                }

                scan.skipWhitespace();
            }

            scan.consume(';');
            SVGParser.processStyleProperty(ruleStyle, propertyName, propertyValue);
            scan.skipWhitespace();
            if (scan.consume('}')) {
                return ruleStyle;
            }
        } while(!scan.empty());

        throw new SAXException("Malformed rule set in <style> element");
    }

    protected static List<String> parseClassAttribute(String val) throws SAXException {
        CSSParser.CSSTextScanner scan = new CSSParser.CSSTextScanner(val);
        ArrayList classNameList = null;

        while(!scan.empty()) {
            String className = scan.nextIdentifier();
            if (className == null) {
                throw new SAXException("Invalid value for \"class\" attribute: " + val);
            }

            if (classNameList == null) {
                classNameList = new ArrayList();
            }

            classNameList.add(className);

            scan.skipWhitespace();
        }

        return classNameList;
    }

    protected static boolean ruleMatch(CSSParser.Selector selector, SvgElementBase obj) {
        List<SvgContainer> ancestors = new ArrayList();

        for(SvgContainer parent = obj.parent; parent != null; parent = ((SvgObject)parent).parent) {
            ancestors.add(0, parent);
        }

        int ancestorsPos = ancestors.size() - 1;
        return selector.size() == 1 ? selectorMatch(selector.get(0), ancestors, ancestorsPos, obj)
                                    : ruleMatch(selector, selector.size() - 1, ancestors, ancestorsPos, obj);
    }

    private static boolean ruleMatch(CSSParser.Selector selector, int selPartPos, List<SvgContainer> ancestors, int ancestorsPos, SvgElementBase obj) {
        CSSParser.SimpleSelector sel = selector.get(selPartPos);
        if (!selectorMatch(sel, ancestors, ancestorsPos, obj)) {
            return false;
        } else if (sel.combinator != CSSParser.Combinator.DESCENDANT) {
            if (sel.combinator == CSSParser.Combinator.CHILD) {
                return ruleMatchOnAncestors(selector, selPartPos - 1, ancestors, ancestorsPos);
            } else {
                int childPos = getChildPosition(ancestors, ancestorsPos, obj);
                if (childPos <= 0) {
                    return false;
                } else {
                    SvgElementBase prevSibling = (SvgElementBase)obj.parent.getChildren().get(childPos - 1);
                    return ruleMatch(selector, selPartPos - 1, ancestors, ancestorsPos, prevSibling);
                }
            }
        } else if (selPartPos == 0) {
            return true;
        } else {
            while(ancestorsPos >= 0) {
                if (ruleMatchOnAncestors(selector, selPartPos - 1, ancestors, ancestorsPos)) {
                    return true;
                }
                --ancestorsPos;
            }
            return false;
        }
    }

    private static boolean ruleMatchOnAncestors(CSSParser.Selector selector, int selPartPos, List<SvgContainer> ancestors, int ancestorsPos) {
        CSSParser.SimpleSelector sel = selector.get(selPartPos);
        SvgElementBase obj = (SvgElementBase)ancestors.get(ancestorsPos);
        if (!selectorMatch(sel, ancestors, ancestorsPos, obj)) {
            return false;
        } else if (sel.combinator != CSSParser.Combinator.DESCENDANT) {
            if (sel.combinator == CSSParser.Combinator.CHILD) {
                return ruleMatchOnAncestors(selector, selPartPos - 1, ancestors, ancestorsPos - 1);
            } else {
                int childPos = getChildPosition(ancestors, ancestorsPos, obj);
                if (childPos <= 0) {
                    return false;
                } else {
                    SvgElementBase prevSibling = (SvgElementBase)obj.parent.getChildren().get(childPos - 1);
                    return ruleMatch(selector, selPartPos - 1, ancestors, ancestorsPos, prevSibling);
                }
            }
        } else if (selPartPos == 0) {
            return true;
        } else {
            while(ancestorsPos > 0) {
                int var10001 = selPartPos - 1;
                --ancestorsPos;
                if (ruleMatchOnAncestors(selector, var10001, ancestors, ancestorsPos)) {
                    return true;
                }
            }

            return false;
        }
    }

    private static int getChildPosition(List<SvgContainer> ancestors, int ancestorsPos, SvgElementBase obj) {
        if (ancestorsPos < 0) {
            return -1;
        } else if (ancestors.get(ancestorsPos) != obj.parent) {
            return -1;
        } else {
            int childPos = 0;

            for(Iterator var5 = obj.parent.getChildren().iterator(); var5.hasNext(); ++childPos) {
                SvgObject child = (SvgObject)var5.next();
                if (child == obj) {
                    return childPos;
                }
            }

            return -1;
        }
    }

    private static boolean selectorMatch(CSSParser.SimpleSelector sel, List<SvgContainer> ancestors, int ancestorsPos, SvgElementBase obj) {
        if (sel.tag != null) {
            if (sel.tag.equalsIgnoreCase("G")) {
                if (!(obj instanceof Group)) {
                    return false;
                }
            } else if (!sel.tag.equals(obj.getClass().getSimpleName().toLowerCase(Locale.US))) {
                return false;
            }
        }

        Iterator var5;
        if (sel.attribs != null) {
            var5 = sel.attribs.iterator();

            while(var5.hasNext()) {
                CSSParser.Attrib attr = (CSSParser.Attrib)var5.next();
                if (attr.name == "id") {
                    if (!attr.value.equals(obj.id)) {
                        return false;
                    }
                } else {
                    if (attr.name != "class") {
                        return false;
                    }

                    if (obj.classNames == null) {
                        return false;
                    }

                    if (!obj.classNames.contains(attr.value)) {
                        return false;
                    }
                }
            }
        }

        if (sel.pseudos != null) {
            var5 = sel.pseudos.iterator();

            while(var5.hasNext()) {
                String pseudo = (String)var5.next();
                if (!pseudo.equals("first-child")) {
                    return false;
                }

                if (getChildPosition(ancestors, ancestorsPos, obj) != 0) {
                    return false;
                }
            }
        }

        return true;
    }

    public static class Attrib {
        public String name = null;
        public CSSParser.AttribOp operation;
        public String value = null;

        public Attrib(String name, CSSParser.AttribOp op, String value) {
            this.name = name;
            this.operation = op;
            this.value = value;
        }
    }

    private static enum AttribOp {
        EXISTS,
        EQUALS,
        INCLUDES,
        DASHMATCH;

        private AttribOp() {
        }
    }

    private static class CSSTextScanner extends TextScanner {
        public CSSTextScanner(String input) {
            super(input.replaceAll("(?s)/\\*.*?\\*/", ""));
        }

        public String nextIdentifier() {
            int end = this.scanForIdentifier();
            if (end == this.position) {
                return null;
            } else {
                String result = this.input.substring(this.position, end);
                this.position = end;
                return result;
            }
        }

        private int scanForIdentifier() {
            if (this.empty()) {
                return this.position;
            } else {
                int start = this.position;
                int lastValidPos = this.position;
                int ch = this.input.charAt(this.position);
                if (ch == 45) {
                    ch = this.advanceChar();
                }

                if (ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122 || ch == 95) {
                    for(ch = this.advanceChar();
                        ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122 || ch >= 48 && ch <= 57 || ch == 45 || ch == 95;
                        ch = this.advanceChar()) {

                    }

                    lastValidPos = this.position;
                }

                this.position = start;
                return lastValidPos;
            }
        }

        public boolean nextSimpleSelector(CSSParser.Selector selector) throws SAXException {
            if (this.empty()) {
                return false;
            } else {
                int start = this.position;
                CSSParser.Combinator combinator = null;
                CSSParser.SimpleSelector selectorPart = null;
                if (!selector.isEmpty()) {
                    if (this.consume('>')) {
                        combinator = CSSParser.Combinator.CHILD;
                        this.skipWhitespace();
                    } else if (this.consume('+')) {
                        combinator = CSSParser.Combinator.FOLLOWS;
                        this.skipWhitespace();
                    }
                }

                String attrName;
                if (this.consume('*')) {
                    selectorPart = new CSSParser.SimpleSelector(combinator, (String)null);
                } else {
                    attrName = this.nextIdentifier();
                    if (attrName != null) {
                        selectorPart = new CSSParser.SimpleSelector(combinator, attrName);
                        selector.addedElement();
                    }
                }

                while(!this.empty()) {
                    if (this.consume('.')) {
                        if (selectorPart == null) {
                            selectorPart = new CSSParser.SimpleSelector(combinator, (String)null);
                        }

                        attrName = this.nextIdentifier();
                        if (attrName == null) {
                            throw new SAXException("Invalid \".class\" selector in <style> element");
                        }

                        selectorPart.addAttrib("class", CSSParser.AttribOp.EQUALS, attrName);
                        selector.addedAttributeOrPseudo();
                    } else {
                        if (this.consume('#')) {
                            if (selectorPart == null) {
                                selectorPart = new CSSParser.SimpleSelector(combinator, (String)null);
                            }

                            attrName = this.nextIdentifier();
                            if (attrName == null) {
                                throw new SAXException("Invalid \"#id\" selector in <style> element");
                            }

                            selectorPart.addAttrib("id", CSSParser.AttribOp.EQUALS, attrName);
                            selector.addedIdAttribute();
                        }

                        if (selectorPart == null) {
                            break;
                        }

                        if (!this.consume('[')) {
                            if (this.consume(':')) {
                                int pseudoStart = this.position;
                                if (this.nextIdentifier() != null) {
                                    if (this.consume('(')) {
                                        this.skipWhitespace();
                                        if (this.nextIdentifier() != null) {
                                            this.skipWhitespace();
                                            if (!this.consume(')')) {
                                                this.position = pseudoStart - 1;
                                                break;
                                            }
                                        }
                                    }

                                    selectorPart.addPseudo(this.input.substring(pseudoStart, this.position));
                                    selector.addedAttributeOrPseudo();
                                }
                            }
                            break;
                        }

                        this.skipWhitespace();
                        attrName = this.nextIdentifier();
                        String attrValue = null;
                        if (attrName == null) {
                            throw new SAXException("Invalid attribute selector in <style> element");
                        }

                        this.skipWhitespace();
                        CSSParser.AttribOp op = null;
                        if (this.consume('=')) {
                            op = CSSParser.AttribOp.EQUALS;
                        } else if (this.consume("~=")) {
                            op = CSSParser.AttribOp.INCLUDES;
                        } else if (this.consume("|=")) {
                            op = CSSParser.AttribOp.DASHMATCH;
                        }

                        if (op != null) {
                            this.skipWhitespace();
                            attrValue = this.nextAttribValue();
                            if (attrValue == null) {
                                throw new SAXException("Invalid attribute selector in <style> element");
                            }

                            this.skipWhitespace();
                        }

                        if (!this.consume(']')) {
                            throw new SAXException("Invalid attribute selector in <style> element");
                        }

                        selectorPart.addAttrib(attrName, op == null ? CSSParser.AttribOp.EXISTS : op, attrValue);
                        selector.addedAttributeOrPseudo();
                    }
                }

                if (selectorPart != null) {
                    selector.add(selectorPart);
                    return true;
                } else {
                    this.position = start;
                    return false;
                }
            }
        }

        private String nextAttribValue() {
            if (this.empty()) {
                return null;
            } else {
                String result = this.nextQuotedString();
                return result != null ? result : this.nextIdentifier();
            }
        }

        public String nextPropertyValue() {
            if (this.empty()) {
                return null;
            } else {
                int start = this.position;
                int lastValidPos = this.position;

                for(int ch = this.input.charAt(this.position); ch != -1 && ch != 59 && ch != 125 && ch != 33 && !this.isEOL(ch); ch = this.advanceChar()) {
                    if (!this.isWhitespace(ch)) {
                        lastValidPos = this.position + 1;
                    }
                }

                if (this.position > start) {
                    return this.input.substring(start, lastValidPos);
                } else {
                    this.position = start;
                    return null;
                }
            }
        }
    }

    private static enum Combinator {
        DESCENDANT,
        CHILD,
        FOLLOWS;

        private Combinator() {
        }
    }

    public static enum MediaType {
        all,
        aural,
        braille,
        embossed,
        handheld,
        print,
        projection,
        screen,
        tty,
        tv;

        private MediaType() {
        }
    }

    public static class Rule {
        public CSSParser.Selector selector = null;
        public Style style = null;

        public Rule(CSSParser.Selector selector, Style style) {
            this.selector = selector;
            this.style = style;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            return sb.append(this.selector).append(" {}").toString();
        }
    }

    public static class Ruleset {
        private List<CSSParser.Rule> rules = null;

        public Ruleset() {
        }

        public void add(CSSParser.Rule rule) {
            if (this.rules == null) {
                this.rules = new ArrayList();
            }

            for(int i = 0; i < this.rules.size(); ++i) {
                CSSParser.Rule nextRule = (CSSParser.Rule)this.rules.get(i);
                if (nextRule.selector.specificity > rule.selector.specificity) {
                    this.rules.add(i, rule);
                    return;
                }
            }

            this.rules.add(rule);
        }

        public void addAll(CSSParser.Ruleset rules) {
            if (rules.rules != null) {
                if (this.rules == null) {
                    this.rules = new ArrayList(rules.rules.size());
                }

                Iterator var3 = rules.rules.iterator();

                while(var3.hasNext()) {
                    CSSParser.Rule rule = (CSSParser.Rule)var3.next();
                    this.rules.add(rule);
                }

            }
        }

        public List<CSSParser.Rule> getRules() {
            return this.rules;
        }

        public boolean isEmpty() {
            return this.rules == null || this.rules.isEmpty();
        }

        public String toString() {
            if (this.rules == null) {
                return "";
            } else {
                StringBuilder sb = new StringBuilder();
                Iterator var3 = this.rules.iterator();

                while(var3.hasNext()) {
                    CSSParser.Rule rule = (CSSParser.Rule)var3.next();
                    sb.append(rule.toString()).append('\n');
                }

                return sb.toString();
            }
        }
    }

    public static class Selector {
        public List<CSSParser.SimpleSelector> selector = null;
        public int specificity = 0;

        public Selector() {
        }

        public void add(CSSParser.SimpleSelector part) {
            if (this.selector == null) {
                this.selector = new ArrayList();
            }

            this.selector.add(part);
        }

        public int size() {
            return this.selector == null ? 0 : this.selector.size();
        }

        public CSSParser.SimpleSelector get(int i) {
            return (CSSParser.SimpleSelector)this.selector.get(i);
        }

        public boolean isEmpty() {
            return this.selector == null ? true : this.selector.isEmpty();
        }

        public void addedIdAttribute() {
            this.specificity += 10000;
        }

        public void addedAttributeOrPseudo() {
            this.specificity += 100;
        }

        public void addedElement() {
            ++this.specificity;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            Iterator var3 = this.selector.iterator();

            while(var3.hasNext()) {
                CSSParser.SimpleSelector sel = (CSSParser.SimpleSelector)var3.next();
                sb.append(sel).append(' ');
            }

            return sb.append('(').append(this.specificity).append(')').toString();
        }
    }

    private static class SimpleSelector {
        public CSSParser.Combinator combinator = null;
        public String tag = null;
        public List<CSSParser.Attrib> attribs = null;
        public List<String> pseudos = null;

        public SimpleSelector(CSSParser.Combinator combinator, String tag) {
            this.combinator = combinator != null ? combinator : CSSParser.Combinator.DESCENDANT;
            this.tag = tag;
        }

        public void addAttrib(String attrName, CSSParser.AttribOp op, String attrValue) {
            if (this.attribs == null) {
                this.attribs = new ArrayList();
            }

            this.attribs.add(new CSSParser.Attrib(attrName, op, attrValue));
        }

        public void addPseudo(String pseudo) {
            if (this.pseudos == null) {
                this.pseudos = new ArrayList();
            }

            this.pseudos.add(pseudo);
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            if (this.combinator == CSSParser.Combinator.CHILD) {
                sb.append("> ");
            } else if (this.combinator == CSSParser.Combinator.FOLLOWS) {
                sb.append("+ ");
            }

            sb.append(this.tag == null ? "*" : this.tag);
            Iterator var3;
            if (this.attribs != null) {
                for(var3 = this.attribs.iterator(); var3.hasNext(); sb.append(']')) {
                    CSSParser.Attrib attr = (CSSParser.Attrib)var3.next();
                    sb.append('[').append(attr.name);
//                    CSSParser.AttribOp()[attr.operation.ordinal()]
                    switch(attr.operation.ordinal()) {
                        case 2:
                            sb.append('=').append(attr.value);
                            break;
                        case 3:
                            sb.append("~=").append(attr.value);
                            break;
                        case 4:
                            sb.append("|=").append(attr.value);
                    }
                }
            }

            if (this.pseudos != null) {
                var3 = this.pseudos.iterator();

                while(var3.hasNext()) {
                    String pseu = (String)var3.next();
                    sb.append(':').append(pseu);
                }
            }

            return sb.toString();
        }
    }
}
